Skip to main content

Using statement

Simplify your code by using the C# using statement. If you have a try-finally statement in which the only code in the finally block is a call to the Dispose method, use a using statement instead.

In the following example, the try-finally statement only calls Dispose in the finally block.

Font font1 = new Font("Arial", 10.0f);

try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
{
((IDisposable)font1).Dispose();
}
}

You can do the same thing with a using statement.

using (Font font2 = new Font("Arial", 10.0f))
{
byte charset2 = font2.GdiCharSet;
}